package spring.cloud.tasks.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import spring.cloud.tasks.api.service.NameSpaceAndTaskService;
import spring.cloud.tasks.api.service.ZooKeeperService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Ray Leung
 */
@RequestMapping("/api")
public class NameSpaceAndTaskController {
    @Resource
    private ZooKeeperService zooKeeperService;
    @Autowired
    private NameSpaceAndTaskService nameSpaceAndTaskService;

    //@Audit(type = AuditType.REST)
    @RequestMapping(value = "/namespaces/createNameSpaceAndCloneTasks", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> createNameSpaceAndCloneTasks(@RequestBody Map<String, Object> reqParams, HttpServletRequest request)
            throws RuntimeException {
        try {
            String namespace = (String) reqParams.get("namespace");
            if (StringUtils.isBlank(namespace)) {
                throw new RuntimeException("namespace is empty");
            }
            String zkClusterName = (String) reqParams.get("zkCluster");
            if (StringUtils.isBlank(zkClusterName)) {
                throw new RuntimeException("zkCluster is empty");
            }
            String srcNamespace = (String) reqParams.get("srcNamespace");
            if (StringUtils.isBlank(srcNamespace)) {
                throw new RuntimeException("srcNamespace is empty");
            }
            String createBy = (String) reqParams.get("createBy");
            nameSpaceAndTaskService.createNameSpaceAndCloneTasks(srcNamespace, namespace, zkClusterName, createBy);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    //@Audit(type = AuditType.REST)
    @RequestMapping(value = "/namespaces/asyncCreateNameSpaceAndCloneTasks", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> asyncCreateNameSpaceAndCloneTasks(@RequestBody Map<String, Object> reqParams, HttpServletRequest request)
            throws RuntimeException {
        String namespace = (String) reqParams.get("namespace");
        if (StringUtils.isBlank(namespace)) {
            throw new RuntimeException("namespace is empty");
        }
        String zkClusterName = (String) reqParams.get("zkClusterName");
        if (StringUtils.isBlank(zkClusterName)) {
            throw new RuntimeException("zkCluster is empty");
        }
        String srcNamespace = (String) reqParams.get("srcNamespace");
        if (StringUtils.isBlank(srcNamespace)) {
            throw new RuntimeException("srcNamespace is empty");
        }
        String createBy = (String) reqParams.get("createBy");
        nameSpaceAndTaskService.asyncCreateNameSpaceAndCloneTasks(srcNamespace, namespace, zkClusterName, createBy);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}
